

package view;

import controller.CtrCaixaVenda;
import javax.swing.JOptionPane;
import model.CaixaVenda;
import controller.CtrCadastroFuncionario;
import model.Funcionario;
import java.util.List;
import utils.ExcecaoSCEV;


/**
 *
 * @author  Hélio
 */
public class ProprCaixaVendaJDialog extends javax.swing.JDialog {
    int operacao;
    private int modo = 1;
    CtrCaixaVenda CtrCaixaVenda = new CtrCaixaVenda();
    CaixaVenda umCaixaVenda;
     CaixaVenda caixavenda;


    CtrCadastroFuncionario ctrCadastroFuncionario = CtrCadastroFuncionario.getInstance() ;
    Funcionario funcionario;
    List<Funcionario> funcionarios;
    
    /** Creates new form ProprCaixaVendaJDialog */
    public ProprCaixaVendaJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        try {
            funcionarios = ctrCadastroFuncionario.listarTodos(1);
            carregarFuncionario();
        } catch (ExcecaoSCEV ex) {
            JOptionPane.showMessageDialog(null, ex.getMsg(), "Validação", JOptionPane.ERROR_MESSAGE);
        }
    }
    private void carregarFuncionario() {

        int posicao = 0;
        jComboBoxFuncionario.removeAllItems();
        int i = 0;
        if (!funcionarios.isEmpty()) {
            for (Funcionario umFuncionario : funcionarios) {
                jComboBoxFuncionario.addItem(umFuncionario.getCodigoFuncionario() + "-" + umFuncionario.getNome());
                if (operacao != 0) {
                    if (caixavenda.getFuncionario().getCodigoFuncionario() == umFuncionario.getCodigoFuncionario()) {
                        posicao = i;
                    }
                }
                i++;
            }
            jComboBoxFuncionario.setSelectedIndex(posicao);
        }
 }
    

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okJButton = new javax.swing.JButton();
        cancelarJButton = new javax.swing.JButton();
        jLabelcodigoVenda = new javax.swing.JLabel();
        JTextFieldCodigo = new javax.swing.JTextField();
        jLabelDataVenda = new javax.swing.JLabel();
        jComboBoxDia = new javax.swing.JComboBox();
        jLabelDia = new javax.swing.JLabel();
        jLabelMes = new javax.swing.JLabel();
        jLabelAno = new javax.swing.JLabel();
        jComboBoxMes = new javax.swing.JComboBox();
        jComboBoxAno = new javax.swing.JComboBox();
        jLabelForPagto = new javax.swing.JLabel();
        jLabelVaVenda = new javax.swing.JLabel();
        jLabelVaDesc = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTextFieldVavenda = new javax.swing.JTextField();
        jTextFieldVaDesconto = new javax.swing.JTextField();
        jTextFieldApagto = new javax.swing.JTextField();
        jComboBoxForPagto = new javax.swing.JComboBox();
        jButtonIgual = new javax.swing.JButton();
        jComboBoxFuncionario = new javax.swing.JComboBox();
        jLabelFuncionario = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Propriedades do Fornecedor");

        okJButton.setText("OK");
        okJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okJButtonActionPerformed(evt);
            }
        });

        cancelarJButton.setText("Cancelar");
        cancelarJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarJButtonActionPerformed(evt);
            }
        });

        jLabelcodigoVenda.setText("Código venda:");

        JTextFieldCodigo.setEnabled(false);

        jLabelDataVenda.setText("Data da venda");

        jComboBoxDia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));

        jLabelDia.setText("dia");

        jLabelMes.setText("mês");

        jLabelAno.setText("ano");

        jComboBoxMes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro" }));

        jComboBoxAno.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025" }));

        jLabelForPagto.setText("Forma de pagamento");

        jLabelVaVenda.setText("Valor venda");

        jLabelVaDesc.setText("Valor desconto");

        jLabel11.setText("A pagar");

        jTextFieldVavenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldVavendaActionPerformed(evt);
            }
        });
        jTextFieldVavenda.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldVavendaFocusLost(evt);
            }
        });

        jTextFieldVaDesconto.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldVaDescontoFocusLost(evt);
            }
        });

        jComboBoxForPagto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dinheiro", "Cartão", "Cheque" }));

        jButtonIgual.setText("=");
        jButtonIgual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonIgualActionPerformed(evt);
            }
        });

        jComboBoxFuncionario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxFuncionarioActionPerformed(evt);
            }
        });

        jLabelFuncionario.setText("Funcionário");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(jLabelDia)
                .addContainerGap(242, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBoxForPagto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelDataVenda)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelcodigoVenda)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addComponent(jComboBoxDia, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabelVaVenda)
                            .addComponent(jTextFieldVavenda, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jComboBoxMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jLabelVaDesc, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jTextFieldVaDesconto, javax.swing.GroupLayout.Alignment.LEADING)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(14, 14, 14)
                                        .addComponent(jComboBoxAno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jButtonIgual)
                                        .addGap(10, 10, 10)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel11)
                                            .addComponent(jTextFieldApagto, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addComponent(JTextFieldCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(125, 125, 125)
                        .addComponent(jLabelMes)
                        .addGap(44, 44, 44)
                        .addComponent(jLabelAno))
                    .addComponent(jLabelFuncionario)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(okJButton)
                            .addComponent(jLabelForPagto))
                        .addGap(18, 18, 18)
                        .addComponent(cancelarJButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelarJButton, okJButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelcodigoVenda)
                    .addComponent(JTextFieldCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelFuncionario)
                    .addComponent(jComboBoxFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelDataVenda)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDia)
                    .addComponent(jLabelMes)
                    .addComponent(jLabelAno))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxDia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxAno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelVaVenda)
                    .addComponent(jLabelVaDesc)
                    .addComponent(jLabel11))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldVavenda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldVaDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonIgual)
                    .addComponent(jTextFieldApagto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addComponent(jLabelForPagto)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxForPagto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okJButton)
                    .addComponent(cancelarJButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cancelarJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarJButtonActionPerformed
    dispose();//GEN-LAST:event_cancelarJButtonActionPerformed
}

private void okJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okJButtonActionPerformed
    umCaixaVenda.setDiaVenda(Integer.parseInt((String) jComboBoxDia.getSelectedItem()));
    umCaixaVenda.setAnoVenda(Integer.parseInt((String) jComboBoxAno.getSelectedItem()));
    umCaixaVenda.setValorVenda(Double.parseDouble( jTextFieldVavenda.getText()));
    umCaixaVenda.setValorDesconto(Double.parseDouble( jTextFieldVaDesconto.getText()));
    umCaixaVenda.setaPagto(Double.parseDouble( jTextFieldApagto.getText()));
    umCaixaVenda.setFormaPagto(jComboBoxForPagto.getSelectedItem().toString());
    umCaixaVenda.setMesVenda(jComboBoxMes.getSelectedItem().toString());
    umCaixaVenda.setFuncionario(funcionarios.get(jComboBoxFuncionario.getSelectedIndex()));
    if (CtrCaixaVenda.persistir(umCaixaVenda)) {
            JOptionPane.showMessageDialog(null, "Venda realizada com sucesso.", "Caixa de venda", JOptionPane.INFORMATION_MESSAGE);
    dispose();
    }    
    else 
    {
    JOptionPane.showMessageDialog(null, "Dados inválidos.", "Caixa de venda", JOptionPane.ERROR_MESSAGE);
    }
        
    
}//GEN-LAST:event_okJButtonActionPerformed

private void jButtonIgualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonIgualActionPerformed
   Double num1, num2, resultado;
      num1 = Double.parseDouble(jTextFieldVavenda.getText());
      num2 = Double.parseDouble(jTextFieldVaDesconto.getText());
      resultado = num1-num2;
      String r = String.valueOf(resultado);
      jTextFieldApagto.setText(r);

   
}//GEN-LAST:event_jButtonIgualActionPerformed

private void jComboBoxFuncionarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxFuncionarioActionPerformed
    
}//GEN-LAST:event_jComboBoxFuncionarioActionPerformed

private void jTextFieldVavendaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldVavendaFocusLost
    
}//GEN-LAST:event_jTextFieldVavendaFocusLost

private void jTextFieldVaDescontoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldVaDescontoFocusLost
    
}//GEN-LAST:event_jTextFieldVaDescontoFocusLost

private void jTextFieldVavendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldVavendaActionPerformed
    
}//GEN-LAST:event_jTextFieldVavendaActionPerformed
                                               
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField JTextFieldCodigo;
    private javax.swing.JButton cancelarJButton;
    private javax.swing.JButton jButtonIgual;
    private javax.swing.JComboBox jComboBoxAno;
    private javax.swing.JComboBox jComboBoxDia;
    private javax.swing.JComboBox jComboBoxForPagto;
    private javax.swing.JComboBox jComboBoxFuncionario;
    private javax.swing.JComboBox jComboBoxMes;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabelAno;
    private javax.swing.JLabel jLabelDataVenda;
    private javax.swing.JLabel jLabelDia;
    private javax.swing.JLabel jLabelForPagto;
    private javax.swing.JLabel jLabelFuncionario;
    private javax.swing.JLabel jLabelMes;
    private javax.swing.JLabel jLabelVaDesc;
    private javax.swing.JLabel jLabelVaVenda;
    private javax.swing.JLabel jLabelcodigoVenda;
    private javax.swing.JTextField jTextFieldApagto;
    private javax.swing.JTextField jTextFieldVaDesconto;
    private javax.swing.JTextField jTextFieldVavenda;
    private javax.swing.JButton okJButton;
    // End of variables declaration//GEN-END:variables

    public int getModo() {
        return modo;
    }

    public void setModo(int modo) {
        this.modo = modo;
        switch( this.modo ) {
        case 1 :
            setTitle("Caixa aberto!");
            JTextFieldCodigo.setText("AUTO");
            umCaixaVenda = new CaixaVenda();
            break;
        
        }
    }

public void setValorTotal(Double valorTotal){
jTextFieldVavenda.setText(valorTotal.toString());
}
}
